package com.hsd.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hsd.model.Device;
import com.hsd.service.DeviceService;
import com.hsd.util.AjaxJson;
import com.hsd.util.JsonConversion;


@Controller
public class DeviceController {
     
    @Resource
    private DeviceService deviceService;
   @InitBinder  
    public void initBinder(WebDataBinder binder) {  
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
            dateFormat.setLenient(false);  
            binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));  
    }
    
    @RequestMapping(value ="device")
    public String device(){
        
        return "device/device";
        
    }
    @RequestMapping(value ="add")
    public String addDevice(){
        
        return "device/addDevice";
        
    }
    /*@RequestMapping(value ="edit")
    public String details(){
        
        return "device/edit";
    }*/
    
    @ResponseBody
    @RequestMapping(value="/getAllDevice",produces = "application/json; charset=UTF-8")
    public String getAllDevice(@RequestParam(value="page") int page,
            @RequestParam(value="limit") int limit,HttpServletRequest request,  Model model){
        int start =(page-1)*limit+1;
        int end = page *limit;
        String imei = request.getParameter("imei");
        List<Device> device;
        int count ;
        if(imei != null && imei != ""){
             device = deviceService.searchDeviceByImei(start,end,imei);
             count = deviceService.getCountByImei();
            
        }else{
            device  =deviceService.getAllDevice(start,end);
            count = deviceService.getCount();
            
        }
       
        
        Map resultMap = new HashMap<>();    
        if(device != null && device.size()>0) {
            resultMap.put("code",0);

            resultMap.put("msg","success");

            resultMap.put("count",count);

            resultMap.put("data",device);
       }else{
            resultMap.put("code",-1);

            resultMap.put("msg","无数据");

            resultMap.put("count",count);

            resultMap.put("data","");
        }
       return JsonConversion.writeMapJSON(resultMap);
       }
    @RequestMapping("/edit")    
    public ModelAndView edit(HttpSession session){      
        ModelAndView mav = new ModelAndView("device/edit");
       
        return mav;    
    }
  @ResponseBody
    @RequestMapping(value="/updateDeviceByImei",produces="application/json;charset=utf-8")
    public AjaxJson updateDeviceByImei(Device device){
        AjaxJson j = new AjaxJson();
        
        try{
            deviceService.updateDeviceByImei(device);
            
            j.setSuccess(true);
            j.setMsg("修改成功");
            
        } catch (Exception e) {
            e.printStackTrace();
            j.setSuccess(false);
            
        }
        return j;
        
    }
  @ResponseBody
  @RequestMapping(value="/addDevice",produces="application/json;charset=utf-8")
  public AjaxJson addDevice(Device device){
      AjaxJson j = new AjaxJson();
      
      try{
          deviceService.addDevice(device);
          
          j.setSuccess(true);
          j.setMsg("添加成功");
          
      } catch (Exception e) {
          e.printStackTrace();
          j.setSuccess(false);
          
      }
      return j;
      
  }
    
  
}
    